#!/bin/bash

# shellcheck source=meta-facebook/meta-minerva/recipes-minerva/plat-tool/files/minerva-common-functions
source /usr/libexec/minerva-common-functions

# Minerva CMM Sled Power Cycle and Chassis Power Cycle

cmm-hsc-power-cycle() {
    #TBD: Since there has a 2nd HSC, it is necessary to determine which HSC it is now.

    # LTC4287 MFR_REBOOT_CONTROL (FDh) Read/Write
    # [3]:
    #       REBOOT 0 Write a 1 to reboot.
    # [2:0]
    #       RBT_DL 100 Configures Auto-Reboot turn-on Delay (tDL(RBT)) after the REBOOT bit is set to 1
    #       RBT_DL[2:0] DELAY
    #       000 0.580 s
    #       001 1.16 s
    #       010 2.32 s
    #       011 4.64 s
    #       100 9.28 s
    #       101 18.6 s
    #       110 37.1 s
    #       111 74.2 s
    i2cset -y -f 0 0x44 0xfd 0x00
    i2cset -y -f 0 0x44 0xfd 0x0b
}

compute-blade-hsc-power-off() {
    for i in {0..15}
    do
        set_gpio "PWREN_MTIA_BLADE${i}_HSC_EN_N" 1
    done
}

network-blade-hsc-power-off() {
    for i in {0..15}
    do
        set_gpio "PWREN_MTIA_BLADE${i}_HSC_EN_N" 1
    done
}

# CMM Sled Power Cycle
if [ "$1" == 0 ]
then
    echo "Staring CMM Sled Power Cycle"
    cmm-hsc-power-cycle
# CMM Chassis Power Cycle
elif [ "$1" == 1 ]
then
    echo "Staring CMM Chassis Power Cycle"

    compute-blade-hsc-power-off
    network-blade-hsc-power-off
    cmm-hsc-power-cycle
else
    echo "Invalid CMM Cycle"
    exit 1
fi
